package com.example.combwx.db;

import android.content.Context;

public class UserUtility {
    private DaoMaster daoMaster;
    private DaoSession daoSession;
    private Context context;
    public void init(Context context){
        this.context = context;
    }
    public DaoMaster getDaoMaster() throws Exception {
        if (context == null){
            throw new Exception("context = null");
        }
        if (daoMaster == null){
            DaoMaster.DevOpenHelper user = new DaoMaster.DevOpenHelper(context, "user");
            daoMaster = new DaoMaster(user.getWritableDatabase());
        }
        return daoMaster;
    }
    public DaoSession getDaoSession() throws Exception {
        if (daoSession == null){
            daoSession =getDaoMaster().newSession();
        }
        return daoSession;
    }
    public void close(){
        if (daoSession != null){
            daoSession.clear();
            daoSession = null;
        }
        if (daoMaster !=null){
            daoMaster = null;
        }
    }
    public static UserUtility userUtility;
    public static UserUtility getInstance(){
        if (userUtility == null){
            userUtility = new UserUtility();
        }
        return userUtility;
    }

}
